package top.codedance.iotp.server.net.handle.util;

import top.codedance.iotp.common.entity.UserEntity;
import top.codedance.iotp.server.ServerApplication;
import top.codedance.iotp.server.net.thrid.UserThrid;
import top.codedance.iotp.server.net.thrid.user.ChongQingUserImpl;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

public class UserService {
    private static Logger logger = ServerApplication.getLogger(UserService.class);

    private static UserThrid userThrid = new ChongQingUserImpl();

    /**
     * 检测用户是否存在
     * @param userEntity
     * @return
     */
    public static boolean isExits(UserEntity userEntity){
        try {
            if(ServerApplication.userThridUserService){
                return userThrid.isExits(userEntity);
            }else{
                return ServerApplication.userDaoUtil.has(userEntity);
            }
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return false;
        }
    }

    /**
     * 检测用户是否存在，使用公钥解密
     * @param userEntity
     * @return
     */
    public static boolean isExits(UserEntity userEntity, String privateKey){
        try {
            if(ServerApplication.userThridUserService){
                return userThrid.isExits(userEntity);
            }else{
                return ServerApplication.userDaoUtil.has(userEntity, privateKey);
            }
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return false;
        }
    }

    /**
     * 检测用户是否存在
     * @param userEntity
     * @return
     */
    public static boolean isExitsOfAdmin(UserEntity userEntity){
        try {
            if(ServerApplication.userThridUserService){
                return userThrid.isExitsOfAdmin(userEntity);
            }else{
                return ServerApplication.userDaoUtil.hasAdmin(userEntity);
            }
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return false;
        }
    }

    /**
     * 检测用户是否存在，使用公钥解密
     * @param userEntity
     * @return
     */
    public static boolean isExitsOfAdmin(UserEntity userEntity, String privateKey){
        try {
            if(ServerApplication.userThridUserService){
                return userThrid.isExitsOfAdmin(userEntity);
            }else{
                return ServerApplication.userDaoUtil.hasAdmin(userEntity, privateKey);
            }
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return false;
        }
    }

    /**
     * 添加用户
     * @param userEntity
     */
    public static void add(UserEntity userEntity){
        try {
            if(ServerApplication.userThridUserService){
                userThrid.add(userEntity);
            }else {
                ServerApplication.userDaoUtil.add(userEntity);
            }
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }

    /**
     * 获取用户
     * @param username
     * @return
     */
    public static UserEntity getUser(String username){
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(username);
        try {
            return (UserEntity) ServerApplication.userDaoUtil.getEntity(userEntity);
        }catch (Exception ex){
            logger.debug(ex.getMessage());
            return null;
        }
    }

    /**
     * 获取用户
     * @param username
     * @return
     */
    public static UserEntity getUser(String username, String privateKey){
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername(username);
        try {
            return (UserEntity) ServerApplication.userDaoUtil.getEntity(userEntity, privateKey);
        }catch (Exception ex){
            logger.debug(ex.getMessage());
            return null;
        }
    }

    /**
     * 查询所有用户
     * @return
     */
    public static List<UserEntity> list(){
        try {
            if(ServerApplication.userThridUserService){
                return userThrid.list();
            }else {
                return ServerApplication.userDaoUtil.selectAll();
            }
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return new ArrayList<UserEntity>();
        }
    }

    /**
     * 查询所有用户
     * @return
     */
    public static List<UserEntity> list(String privateKey){
        try {
            if(ServerApplication.userThridUserService){
                return userThrid.list();
            }else {
                return ServerApplication.userDaoUtil.selectAll(privateKey);
            }
        } catch (Exception e) {
            logger.debug(e.getMessage());
            return new ArrayList<UserEntity>();
        }
    }

    /**
     * 删除用户
     * @param id
     */
    public static void delete(int id){
        try {
            if(ServerApplication.userThridUserService){
                userThrid.delete(id);
            }else {
                ServerApplication.userDaoUtil.delete(id);
            }
        } catch (Exception e) {
            logger.debug(e.getMessage());
        }
    }
}
